import { JoinClient } from 'meteor-publish-join';
import { clientChannelApps } from '../../lib/collections';

Template.channel_apps_panel.onCreated(function () {
  this.limit = new ReactiveVar(15);

  this.autorun(() => {
    this.resultsSub = this.subscribe('get.channel.apps',{ channelId: this.data.channel._id }, this.limit.get());
    this.total = JoinClient.get('get.channel.apps.counts');
  });
});

Template.channel_apps_panel.helpers({
  results: function () {
    return clientChannelApps.find({
      channelId: Template.instance().data.channel._id
    }, {
      limit: Template.instance().limit.get(),
      sort: {
        createdAt: -1
      }
    });
  },

  loading: function () {
    return !Template.instance().resultsSub.ready();
  }
});

Template.channel_apps_panel.events({
  'click #open-app-store' () {
    $('#app-store').modal();
  }

});